import machine
from machine import ADC, Pin,I2C, deepsleep
from machine import RTC
import utime
import esp32
import IIC256
import DS3231
SHT31_address       =   68
adc = ADC(Pin(34))
adc.atten(ADC.ATTN_11DB)    # set 11dB input attenuation (voltage range roughly 0.0v - 3.6v)
adc.width(ADC.WIDTH_12BIT)  
# v=adc.read()
# print(v)
# print(v*7.36/4096)
i2c = I2C(1, scl=Pin(18), sda=Pin(19), freq=400000)
wake1 = Pin(27, mode = Pin.IN,pull=Pin.PULL_DOWN)
rtc = RTC()

def callback1(str):
    print(str+"        ")
    IIC256.lprint(str,64,15)

def showFast():
    leds=machine.Pin(14,Pin.IN,Pin.PULL_DOWN)
    showSlow()
    k=0
    for i in range(60):
        if(wake1.value()==1):
            k=k+1
            IIC256.lprint("%02d"%(k),64,15)
        else:
            k=0
            IIC256.lprint("               ",64,15)
        if k==3:

            DS3231.sync(callback1)
        da=i2c.readfrom_mem(104,0,1)
        s=((da[0]>>4) * 10) + (da[0]&0x0f)
        strs=("%02d"%(s))
        if(s==0): 
            showSlow()
        IIC256.lprint(strs,28,1)
        t=rtc.datetime()
        print(i)
        gett()
        utime.sleep_us(1000000-t[7])


        
    IIC256.lprint("              ",27,1)
    leds=machine.Pin(14,machine.Pin.IN,Pin.PULL_UP)
    machine.deepsleep((60-s)*1000)
    # machine.deepsleep(5000)

def showSlow(ds=0):
    da=i2c.readfrom_mem(104,0,7)
    s=((da[0]>>4) * 10) + (da[0]&0x0f)
    m1=da[1]>>4
    m2=da[1]&0x0f
    h1=da[2]>>4
    h2=da[2]&0x0f
    t1=[h1,h2,10,m1,m2]
    d=((da[4]>>4) * 10) + (da[4]&0x0f)
    w=((da[3]>>4) * 10) + (da[3]&0x0f)
    mo=((da[5]>>4) * 10) + (da[5]&0x0f)
    y=((da[6]>>4) * 10) + (da[6]&0x0f)
    IIC256.lprint("20%02d-%02d-%02d"%(y,mo,d),28,2)
    IIC256.lcdAddress(170,4,16,16)
    IIC256.LDS(IIC256.xj[7])
    IIC256.lcdAddress(170+16,4,16,16)
    IIC256.LDS(IIC256.xj[8])
    IIC256.lcdAddress(170+32,4,16,16)
    IIC256.LDS(IIC256.xj[w%7])

    gett()
    n=0
    for t in t1:
        IIC256.lcdAddress(n*32,0,32,64)
        for i in range(16):
            IIC256.LDS(IIC256.bigzf[t*16+i])
        n=n+1
    if ds==1 :
        da=i2c.readfrom_mem(104,0,1)
        s=((da[0]>>4) * 10) + (da[0]&0x0f)
        machine.deepsleep((60-s)*1000)
def gett():
    i2c.writeto(SHT31_address,b"\x2c\x06")
    utime.sleep_ms(100)
    raw=i2c.readfrom(SHT31_address,6)
    (t,h)=(raw[0] << 8) + raw[1], (raw[3] << 8) + raw[4]
    shtrt = -45 + (175 * (t / 65535))
    shtrh = 100 * (h / 65535)
    IIC256.print32("%.2fC %.2f%%   "%(shtrt,shtrh),0,8)
    v=adc.read()
    IIC256.print32("%.2fV"%(v*7.36/4096),0,12)
    # IIC256.lprint("%d"%(v),32,1)
if machine.wake_reason() == 0:
    cb=b'789cbd58cd6edb46105efe44a400d6545d1d6440b06845681321409c1e04173044d2955db3eda1069c02b9144a9307686f6eab78993a873e860fbef70d0a3a39e431caa0b79ee8536480163bb3bba4488a4e1bc7e947902277b8f33fb34b11c270174e0d8e2685a3d7a7fde5ef1feb1fdd1c78f7bbdf901b0a50db5d9710b7db264d7da83775b8e8841c8f898e20e33121564357e5e19eb73d245bd4215b0ff7bc9d3577fbab7bade6326d7cb2bdbdfdd9e6979eb72efd58bfa10c3ccf1bd43ceff62a190c80fdb770d19b20a38900414d5d96f6eab54fbd077bc3c9f287cb138a3386aeeb76299c6d7c7469bd5e9786aee7fd4c410b4a5cea12cb716d89222fea00a86cc894aa7a83e2dc21cea80dddbdb5ef687de5f6ce0e70b9274954926c78d7ee588ed5b19d0dc77ed4d49b8f34fd075d0333b63f272093dcefc3415c971255966415b93b046e88b5bfbf7f407747a38d8dd16864c165976ec0584b3fac4b72ebe4e4644275553d20a3270e71fc674f086ddc1a813bc14d13b5a51e4c908a7337e25eafd76ac119e398be0b5c2cd51e39d6a6e3589336e8d7de74d69dcdd12de0d03e3e3e9e8c1eef7f3d22fa00c243289cee40273a844227297c3f08424410043e29209cc6b384764c4d41689a6927b3781aa6d4044095e2141b0703768b770929633eaae0bbb3b0400d663066a74f1248b429888c18a67142ed8e262df094041608ff1de8828a611fc6a3e964586d0b21e81a147b193dc5fba65fdd7c0c3f8f3f6680efe7282cfc36849fc75fd1cc0e466c1a5c26d36729616b15628230e2cc4af9821e06311857a56c00930f787d16159137ba0a6fe7804038007ff3f6730764e98f0550b0bfe400746404fad24ed9444813d3ec743a90cb17e751984e673917145f7c9a0fb4a4756c4ae3a948ff88f9a9a200ae667705981b2a293e54018bc5621662fc589390ca59ea63cca90dfe130305fa84027ef9e9200746e7b7955abc872a082a30a7866833e47f11d9dc18f57915a66989e1cc25900e5167fd81e90457051b2874d05048ae707690339137bf2cfad1741a53ba5841574cfb327cd601c6d5a488ad03da6262230914fef3e55f154b4818b37260f583e6b30aa2d91262305bfd12c79c0778fef3f68fdc4505bcc1036fe782aad8e753a01c77c0cbd7d8e0ccaa0e07dd8239a95207d6196cb35abf004b8b5e42e439682fba5ee05d43efe7cd2eb43fecbf49326fffbcff4338523a66731261ce73408fc71d4127b32b8cce2f70c91680743e3f87500a212cd065cd5fe0e084dd2a26843f89a7f3be8fe1877e724de1e7e667ed3f2cdb9fed7eb80360fbd361e52b36408755dd28627b2232d79fb7ef5068cf92672194a0f8e9d945525d613caf705ab575d7d3f9ff05e5be5886c97075fe7cfb7139bdd877ff1fbcc396a21a35d3ac5d039b3cfc003e327cf110cd780b91b419db21d9621f4e7cb1a396949a011f42b57c26f9c1fc1eed2c6774fe19e61aa6892cd2c9ec2327ad9ad515317c9a0c53fa7acb40714cc8d20786c0124f73dfe7d3c5349140fe29e5d331e170d9906b31b347d154f639a9d60a5592cb4b7fdc309454e3f53b1faf598024814bce04d3cc0c28f5fb8414140a5e083f4949cce4ab42febc4ab905c2fe30d69e623c82234ea7c951613e0486f9df48f52fad37c22b61ab368f4f10173b42b1c858f8337d87f48c2dbfbe4b57e1318a9764b6e9585a65fe83e867fe9372daa7f91327a928653665ef6b067ffd156fe99aa673eddf500c4179475baa9c7c0d845600168c2d4b3850912561ad947e0152c12ce309196c08ff81e181c306d31b2d614b937f90750a3389724f6c641ebb20f51ce37e8869931c92b02dde80c43b7ad856a63345e20e21065224f08a018bbce36c2166ec0a5ffae04c0bb28f28468319e41751e12b7d0e786a144188fa6c0e9510ab088897aaaad989bebad5bb8994b56eb78b2b900c7a3d3f44ed9ecb32bacb710ebe6001c77f3e04bfb5ee9d941f579fe05f16ad3153f08f7ebfff379cbc1c475b1e00ff58f15c74b79af47abd442533903723e4b7ddbbf5facaefbbeba6bcf52bc8ff07acde8399'
    import ubinascii
    s=ubinascii.unhexlify(cb)
    rtc.memory(s)